/**
 * Panel permettant le changement des options
 * 
 * @author Marie-Hélène Goddet, Damien Finck, Cédric Schaller
 * 
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.colorchooser.AbstractColorChooserPanel;

import fastwriting.Config;
import fastwriting.FastWriting;

@SuppressWarnings("serial")
public class JOptions extends JPanel {
	
	private ActionListener actionListener;
	
	protected JPanel jp_centre;
		protected JPanel jp_trait_taille;protected JLabel jl_trait_taille;protected JComboBox jcb_trait_taille;
		protected JPanel jp_trait_couleur;protected JLabel jl_trait_couleur;protected JLabel jl_trait_color;
		protected JPanel jp_LookAndFeel;protected JLabel jl_LookAndFeel;protected JComboBox jcb_LookAndFeel;
		protected JPanel jp_user_nom;protected JLabel jl_user_nom;protected JTextField jtf_user_nom;
		protected JPanel jp_user_mail;protected JLabel jl_user_mail;protected JTextField jtf_user_mail;
		protected JButton jb_save;
	protected JColorChooser jcc;
	
	/**
	 * Constructeur
	 */
	public JOptions() {
		String[] taille_trait = {"fin","moyen","épais"};
		
		UIManager.LookAndFeelInfo[] ui_info = UIManager.getInstalledLookAndFeels();
		String[] ui_tab = new String[ui_info.length+1];
		ui_tab[0]="Système";
		for(int i=1; i<=ui_info.length;i++) {
			ui_tab[i]=ui_info[i-1].getName();
		}
		
		Color c = Config.stringToColor(FastWriting.config.get("Personnalisation").get("CouleurTrait").toString());
		
		/** Événements **/
		ActionListener action = new Event_JOptions(this);
		/** Création des éléments     **/
		this.jp_centre = new JPanel(new GridLayout(8,1));
		
		this.jp_trait_taille = new JPanel();
		this.jp_trait_couleur = new JPanel();
		this.jp_LookAndFeel = new JPanel();
		this.jp_user_nom = new JPanel();
		this.jp_user_mail = new JPanel();

			this.jl_trait_taille = new JLabel("Taille du trait :");this.jcb_trait_taille = new JComboBox(taille_trait);
			this.jl_trait_couleur = new JLabel("Couleur du trait :");this.jl_trait_color = new JLabel(" ███ ");
			this.jl_LookAndFeel = new JLabel("Thème :");this.jcb_LookAndFeel = new JComboBox(ui_tab);
			this.jl_user_nom = new JLabel("Nom :");this.jtf_user_nom = new JTextField(FastWriting.config.get("Utilisateur").get("Nom").toString(),10);
			this.jl_user_mail = new JLabel("E-mail :");this.jtf_user_mail = new JTextField(FastWriting.config.get("Utilisateur").get("Mail").toString(),10);
			this.jb_save = new JButton("Enregistrer");
			
		this.jcc = new JColorChooser(c);
		/** Paramétrage des éléments  **/
		this.jcb_trait_taille.setSelectedItem(FastWriting.config.get("Personnalisation").get("TailleTrait").toString());
		
		int value=Integer.parseInt(FastWriting.config.get("Themes").get("Theme").toString());
		this.jcb_LookAndFeel.setSelectedIndex(value);
		
		Font f = new Font("Monospaced",0,12);
		this.jl_trait_color.setFont(f);
		this.setLayout(new BorderLayout());
		
		this.jcc.setPreviewPanel(this.jl_trait_color);
		this.jl_trait_color.setForeground(c);
		
		//this.jp_right.setEnabled(false);
		//this.jcc.setPreferredSize(new Dimension(450,40));
		
		// paramètres du JColorChooser
		AbstractColorChooserPanel[] panels = this.jcc.getChooserPanels();
		for (int i=0; i<panels.length; i++) {
		  String tmpString = panels[i].getClass().getName();
		  if (tmpString.equals("javax.swing.colorchooser.DefaultRGBChooserPanel") ||
				  tmpString.equals("javax.swing.colorchooser.DefaultSwatchChooserPanel")) {//DefaultHSBChooserPanel
			  this.jcc.removeChooserPanel(panels[i]);
		  }
		}
		/** Action sur les éléments   **/
		this.jb_save.addActionListener(action);
		//this.jcc.addChangeListener(change);
		//this.jb_trait_couleur.addActionListener(action);
		/** Montage des éléments      **/
		this.add(this.jp_centre,BorderLayout.CENTER);
		
			this.jp_centre.add(this.jp_trait_taille);
			this.jp_centre.add(this.jp_trait_couleur);
			this.jp_centre.add(this.jp_LookAndFeel);
			this.jp_centre.add(this.jp_user_nom);
			this.jp_centre.add(this.jp_user_mail);
			this.jp_centre.add(this.jb_save);
			
			this.jp_trait_taille.add(this.jl_trait_taille);
			this.jp_trait_couleur.add(this.jl_trait_couleur);
			this.jp_LookAndFeel.add(this.jl_LookAndFeel);
			this.jp_user_nom.add(this.jl_user_nom);
			this.jp_user_mail.add(this.jl_user_mail);
			
			this.jp_trait_taille.add(this.jcb_trait_taille);
			this.jp_trait_couleur.add(this.jl_trait_color);
			this.jp_LookAndFeel.add(this.jcb_LookAndFeel);
			this.jp_user_nom.add(this.jtf_user_nom);
			this.jp_user_mail.add(this.jtf_user_mail);
			
		this.add(this.jcc,BorderLayout.EAST);
	}
	public void addActionListener(ActionListener actionListener) {
		this.actionListener = actionListener;
	}
	public ActionListener getActionListener() {
		return actionListener;
	}
}
class Event_JOptions implements ActionListener {
	JOptions jp;
	/**
	 * Constructeur
	 * @param jp le panel d'options
	 */
	public Event_JOptions(JOptions jp) {
		this.jp=jp;
	}
	/**
	 * Appelée lors de changement dans les options
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {//java.awt.Color[r=255,g=0,b=0]
		Object obj = arg0.getSource();
		if (obj instanceof JButton) {
			FastWriting.config.get("Utilisateur").put("Nom",jp.jtf_user_nom.getText());
			FastWriting.config.get("Utilisateur").put("Mail",jp.jtf_user_mail.getText());
			FastWriting.config.get("Personnalisation").put("TailleTrait",jp.jcb_trait_taille.getSelectedItem().toString());
			Color temp=jp.jl_trait_color.getForeground();
			FastWriting.config.get("Personnalisation").put("CouleurTrait","java.awt.Color[r="+temp.getRed()+",g="+temp.getGreen()+",b="+temp.getBlue()+"]");
			FastWriting.config.get("Themes").put("Theme",""+jp.jcb_LookAndFeel.getSelectedIndex());

			Config.saveConfig(false);//enregistrement des modifications de config
		}
	}
}